package cn.com.css.taxInspection.attention.service.impl;

import cn.com.css.taxInspection.attention.mapper.JcCfqkcxMapper;
import cn.com.css.taxInspection.attention.service.JcCfqkcxService;
import cn.com.css.taxInspection.gt3.entity.DmGySwryEntity;
import cn.com.css.taxInspection.gt3.entity.JcCfqkcxEntity;
import cn.com.css.taxInspection.gt3.service.DmGySwjgService;
import cn.com.css.taxInspection.gt3.service.DmGySwryService;
import cn.com.css.taxInspection.util.TsBqUtils;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Optional;

@Service
@Slf4j
public class JcCfqkcxServiceImpl extends ServiceImpl<JcCfqkcxMapper, JcCfqkcxEntity> implements JcCfqkcxService {

    @Resource
    private DmGySwjgService dmGySwjgService;

    @Resource
    private DmGySwryService dmGySwryService;

    @Override
    @DS("oracle")
    public IPage<JcCfqkcxEntity> queryCfqkcxList(JcCfqkcxEntity jcCfqkcxEntity) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        IPage<JcCfqkcxEntity> page = new Page<>(jcCfqkcxEntity.getPage(), jcCfqkcxEntity.getLimit());
        LambdaQueryWrapper<JcCfqkcxEntity> queryWrapper = Wrappers.lambdaQuery();
        //案件编号
        if (TsBqUtils.isNotNull(jcCfqkcxEntity.getJcajbh())) {
            queryWrapper.like(JcCfqkcxEntity::getJcajbh, jcCfqkcxEntity.getJcajbh());
        }
        //纳税人识别号
        if (TsBqUtils.isNotNull(jcCfqkcxEntity.getNsrsbh())) {
            queryWrapper.like(JcCfqkcxEntity::getNsrsbh, jcCfqkcxEntity.getNsrsbh());
        }
        //纳税人名称
        if (TsBqUtils.isNotNull(jcCfqkcxEntity.getNsrmc())) {
            queryWrapper.like(JcCfqkcxEntity::getNsrmc, jcCfqkcxEntity.getNsrmc());
        }
        //文书制作日期
        if (TsBqUtils.isNotNull(jcCfqkcxEntity.getStartTime())) {
            queryWrapper.between(JcCfqkcxEntity::getWszzrq, sdf.parse(jcCfqkcxEntity.getStartTime()), sdf.parse(jcCfqkcxEntity.getEndTime()));
        }
        //文书送达日期
        if (TsBqUtils.isNotNull(jcCfqkcxEntity.getSdstartTime())) {
            queryWrapper.between(JcCfqkcxEntity::getWssdsj, sdf.parse(jcCfqkcxEntity.getSdstartTime()), sdf.parse(jcCfqkcxEntity.getSdendTime()));
        }
        queryWrapper.orderByDesc(JcCfqkcxEntity::getJcajbh);
        IPage<JcCfqkcxEntity> iPage = this.page(page, queryWrapper);
        List<JcCfqkcxEntity> JcCfqkcxEntity = iPage.getRecords();
        for (JcCfqkcxEntity cfqkcxEntity : JcCfqkcxEntity) {
            String swrymc = Optional.ofNullable(dmGySwryService.getSwry(cfqkcxEntity.getLrrDmJds()))
                    .map(DmGySwryEntity::getSwryxm)
                    .filter(TsBqUtils::isNotNull)
                    .orElse("暂无");
            cfqkcxEntity.setLrrmc(
                    TsBqUtils.isNotNull(cfqkcxEntity.getLrrDmJds())
                            ? cfqkcxEntity.getLrrDmJds() + "|" + swrymc
                            : swrymc
            );
        }
        return iPage;
    }

}
